package com.woniuxy.processweb.controller;

import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.processdao.model.CountCardStep;
import com.woniuxy.processmodel.dto.CountCardStepDto;
import com.woniuxy.processmodel.form.AddCountCardStepForm;
import com.woniuxy.processmodel.form.UpdateCountStepForm;
import com.woniuxy.processmodel.form.VenueLookCountCardStepForm;
import com.woniuxy.processservice.exception.ProcessException;
import com.woniuxy.processservice.service.CountCardStepService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 次卡流程表 前端控制器
 * </p>
 *
 * @author 冉杰
 *
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/countCardStep")
@Api(tags = {"次卡进度记录接口"})
public class CountCardStepController {
    @Autowired
    private CountCardStepService countCardStepService;
    @ApiOperation("新增次卡进度接口")//次卡订单生成时调用该接口
    @GetMapping("/add")
    public Result addCountCardStep(String orderNum){
        countCardStepService.addCountCardStep(orderNum);
        return Result.success("新增次卡进度成功");
    }
    @ApiOperation("修改次卡进度接口")//次卡课程完成后商家调用该接口
    @PostMapping("/update.venue")
    public Result updateCountCardStep(@RequestBody @Validated UpdateCountStepForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        countCardStepService.updateCountCardStep(form,venueId);
        return Result.success("修改次卡进度成功");
    }
    @ApiOperation("根据id查询次卡进度接口")
    @GetMapping("/look")
    public Result lookCountCardStep(Integer stepId){
        CountCardStep countCardStep = countCardStepService.lookCountCardStep(stepId);
        return Result.success("查询次卡进度成功",countCardStep);
    }
    @ApiOperation("用户查询自己的次卡进度接口")
    @GetMapping("/userLook")
    public Result userLookCountCardStep(@RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        List<CountCardStepDto> dtos = countCardStepService.UserLookCountCardStep(userId);
        return Result.success("查询次卡进度成功",dtos);
    }

    @ApiOperation("商家查询自己的次卡进度接口")
    @PostMapping("/venueLook.venue")
    public Result venueLookCountCardStep(@RequestBody @Validated VenueLookCountCardStepForm form, BindingResult br, @RequestHeader String token){
        if(br.hasErrors()) throw new ProcessException(90001,"参数校验异常");
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        PageInfo<CountCardStepDto> dtos = countCardStepService.venueLookCountCardStep(form,venueId);
        return Result.success("查询次卡进度成功",dtos);
    }





}

